package com.edu.zut.hb.software.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.zut.hb.software.server.mapper.TeacherMapper;
import com.edu.zut.hb.software.server.pojo.Teacher;
import com.edu.zut.hb.software.server.config.security.JwtTokenUtil;
import com.edu.zut.hb.software.server.service.ITeacherService;
import com.edu.zut.hb.software.server.pojo.RespBean;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2021-08-15
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    /**
     * 获取单个教师信息
     * @return
     */
    @Override
    public List<Teacher> getOneTeacher(String name) {
        return teacherMapper.getOneTeacher(name);
    }

    @Override
    public RespBean login(String username, String password, String code, HttpServletRequest request) {
        String captcha = (String) request.getSession().getAttribute("captcha");
        if (!StringUtils.hasText(code) || !captcha.equalsIgnoreCase(code)) {
            return RespBean.error("验证码填写错误！");
        }
        //登录
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("id", username));
        System.out.println(teacher.getPassword());//正确的密码
        System.out.println(password);//输入的密码
        if (!password.equals(teacher.getPassword())) {
            return RespBean.error("用户名或密码不正确!");
        }
        if (!userDetails.isEnabled()){
            return RespBean.error("账号被禁用，请联系管理员!");
        }
        //更新
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        //生成token
        String token = jwtTokenUtil.generateToken(userDetails);
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return RespBean.success("登录成功", tokenMap);
    }
    /**
     * 获取教研室信息
     * @param room
     * @return
     */
    @Override
    public List<Teacher> getOneRoom(String room) {
       return teacherMapper.getOneRoom(room);
    }
    /**
     * 获取教室信息
     * @param classroom
     * @return
     */
    @Override
    public List<Teacher> getOneClass(String classroom) {
        return teacherMapper.getOneClass(classroom);
    }

    @Override
    public Teacher getTeacherByUserName(String username) {
        return teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("id", username));
    }

}
